/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:28 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class KiemtraBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Kiemtra Này";
		private static string EX_STENBAIKIEMTRA_EMPTY="sTenbaikiemtra không được để trống";
		private static string EX_FK_IKHOAHOCID_INVALID="FK_iKhoahocID không hợp lệ";
		private static string EX_FK_IDEKIEMTRAID_INVALID="FK_iDekiemtraID không hợp lệ";
		private static string EX_FK_IHINHTHUCKIEMTRAID_INVALID="FK_iHinhthuckiemtraID không hợp lệ";
		private static string EX_FK_IDANHMUCKIEMTRAID_INVALID="FK_iDanhmuckiemtraID không hợp lệ";
		private static string EX_ITRANGTHAI_INVALID="iTrangthai không hợp lệ";
		private static string EX_DNGAYKIEMTRA_INVALID="dNgaykiemtra không hợp lệ";
		private static string EX_DGIOBATDAUKIEMTRA_INVALID="dGiobatdaukiemtra không hợp lệ";
		private static string EX_ITHOIGIANLAMBAI_INVALID="iThoigianlambai không hợp lệ";
		private static string EX_ID_INVALID="PK_iKiemtraID không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Kiemtra Theo ID
        /// </summary>
        /// <param name="PK_iKiemtraID">Int64:Kiemtra ID</param>
        /// <returns>KiemtraEntity</returns>        
        public static KiemtraEntity GetOne(Int64 PK_iKiemtraID)
        {
            
			if(PK_iKiemtraID<=0)
				throw new Exception(EX_ID_INVALID);
            return KiemtraDAL.GetOne(PK_iKiemtraID);
        }
        /// <summary>
        /// Lấy về List các Kiemtra
        /// </summary>
        /// <returns>List KiemtraEntity:List Kiemtra Cần lấy</returns>
        public static List<KiemtraEntity> GetAll()
        {
            return KiemtraDAL.GetAll();
        }
        public static List<KiemtraEntity> GetByFK_iKhoahocID(Int64 FK_iKhoahocID)
		{
			if(FK_iKhoahocID<=0)
				throw new Exception(EX_FK_IKHOAHOCID_INVALID);
			return KiemtraDAL.GetByFK_iKhoahocID(FK_iKhoahocID);
		}public static List<KiemtraEntity> GetByFK_iDekiemtraID(Int64 FK_iDekiemtraID)
		{
			if(FK_iDekiemtraID<=0)
				throw new Exception(EX_FK_IDEKIEMTRAID_INVALID);
			return KiemtraDAL.GetByFK_iDekiemtraID(FK_iDekiemtraID);
		}public static List<KiemtraEntity> GetByFK_iHinhthuckiemtraID(Int16 FK_iHinhthuckiemtraID)
		{
			if(FK_iHinhthuckiemtraID<=0)
				throw new Exception(EX_FK_IHINHTHUCKIEMTRAID_INVALID);
			return KiemtraDAL.GetByFK_iHinhthuckiemtraID(FK_iHinhthuckiemtraID);
		}public static List<KiemtraEntity> GetByFK_iDanhmuckiemtraID(Int16 FK_iDanhmuckiemtraID)
		{
			if(FK_iDanhmuckiemtraID<=0)
				throw new Exception(EX_FK_IDANHMUCKIEMTRAID_INVALID);
			return KiemtraDAL.GetByFK_iDanhmuckiemtraID(FK_iDanhmuckiemtraID);
		}
       
        /// <summary>
        /// Kiểm tra và thêm mới Kiemtra
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Kiemtra Mới Thêm Vào</returns>
        public static Int32 Add(KiemtraEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return KiemtraDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Kiemtra
        /// </summary>
        /// <param name="entity">KiemtraEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(KiemtraEntity entity)
        {
            checkExist(entity.PK_iKiemtraID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return KiemtraDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Kiemtra
        /// </summary>
        /// <param name="PK_iKiemtraID">Int64 : PK_iKiemtraID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 PK_iKiemtraID)
        {
            checkExist(PK_iKiemtraID);
            return KiemtraDAL.Remove(PK_iKiemtraID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 PK_iKiemtraID)
        {
            KiemtraEntity oKiemtra=KiemtraDAL.GetOne(PK_iKiemtraID);
            if(oKiemtra==null)
                throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">KiemtraEntity: entity</param>
        private static void checkLogic(KiemtraEntity entity)
        {   
			if (String.IsNullOrEmpty(entity.sTenbaikiemtra))
				throw new Exception(EX_STENBAIKIEMTRA_EMPTY);
			if (entity.FK_iKhoahocID < 0)
				throw new Exception(EX_FK_IKHOAHOCID_INVALID);
			if (entity.FK_iDekiemtraID < 0)
				throw new Exception(EX_FK_IDEKIEMTRAID_INVALID);
			if (entity.FK_iHinhthuckiemtraID < 0)
				throw new Exception(EX_FK_IHINHTHUCKIEMTRAID_INVALID);
			if (entity.FK_iDanhmuckiemtraID < 0)
				throw new Exception(EX_FK_IDANHMUCKIEMTRAID_INVALID);
			if (entity.iTrangthai < 0)
				throw new Exception(EX_ITRANGTHAI_INVALID);
			if (DateTime.Parse("1753-01-01")>entity.dNgaykiemtra)
				throw new Exception(EX_DNGAYKIEMTRA_INVALID);
			if (DateTime.Parse("1753-01-01")>entity.dGiobatdaukiemtra)
				throw new Exception(EX_DGIOBATDAUKIEMTRA_INVALID);
			if (entity.iThoigianlambai < 0)
				throw new Exception(EX_ITHOIGIANLAMBAI_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">KiemtraEntity: KiemtraEntity</param>
        private static void checkDuplicate(KiemtraEntity entity,bool checkPK)
        {
            /* 
            Example
            List<KiemtraEntity> list = KiemtraDAL.GetAll();
            if (list.Exists(
                delegate(KiemtraEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.PK_iKiemtraID != entity.PK_iKiemtraID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">KiemtraEntity:entity</param>
        private static void checkFK(KiemtraEntity entity)
        {            
			KhoahocEntity oKhoahoc = KhoahocDAL.GetOne(entity.FK_iKhoahocID);
			if (oKhoahoc==null)
			{
				throw new Exception("Không tìm thấy :FK_iKhoahocID");
			}
			DekiemtraEntity oDekiemtra = DekiemtraDAL.GetOne(entity.FK_iDekiemtraID);
			if (oDekiemtra==null)
			{
				throw new Exception("Không tìm thấy :FK_iDekiemtraID");
			}
			HinhthuckiemtraEntity oHinhthuckiemtra = HinhthuckiemtraDAL.GetOne(entity.FK_iHinhthuckiemtraID);
			if (oHinhthuckiemtra==null)
			{
				throw new Exception("Không tìm thấy :FK_iHinhthuckiemtraID");
			}
			DanhmuckiemtraEntity oDanhmuckiemtra = DanhmuckiemtraDAL.GetOne(entity.FK_iDanhmuckiemtraID);
			if (oDanhmuckiemtra==null)
			{
				throw new Exception("Không tìm thấy :FK_iDanhmuckiemtraID");
			}
        }
        #endregion
    }
}
